// import { previewFile } from '@/api/common'

// 关闭所有窗口
export const modalClosed = (item) => {
  Object.keys(item).forEach((value) => {
    item[value] = false
  })
}

// 判断是否对象是否有空值
export const hasEmptyValue = (object) => {
  var arr = Object.keys(object)
  for (var i of arr) {
    if (object[i] === '' || object[i] == null) {
      return false
    }
  }
  return true
}
// 把js对象转成查询格式
export const toQueryString = (obj) => {
  let str = ''
  for (var n in obj) {
    str += n + '=' + obj[n] + '&'
  }
  str = str.substring(0, str.length - 1)
  return str
}

// 把枚举值转成中文显示返回状态对应中文名称
export const toCN = (items, value) => {
  let data = ''
  for (var j of items) {
    if (j.value === value) {
      data = j.title
    }
  }
  if (data !== '') {
    return data
  } else {
    return '无'
  }
}
// 根据出生日期算年龄 3.27lx
export const getAge = (strAge) => {
  var birArr = strAge.split('-')
  var birYear = birArr[0]
  var birMonth = birArr[1]
  var birDay = birArr[2]

  var d = new Date()
  var nowYear = d.getFullYear()
  var nowMonth = d.getMonth() + 1 // 记得加1
  var nowDay = d.getDate()
  var returnAge

  if (birArr == null) {
    return false
  }
  d = new Date(birYear, birMonth - 1, birDay)
  if (nowYear === birYear) {
    returnAge = 0
  } else {
    var ageDiff = nowYear - birYear
    if (ageDiff > 0) {
      if (nowMonth === birMonth) {
        var dayDiff = nowDay - birDay
        if (dayDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      } else {
        var monthDiff = nowMonth - birMonth
        if (monthDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      }
    } else {
      return '出生日期晚于今天，数据有误' // 返回-1 表示出生日期输入错误 晚于今天
    }
  }
  return returnAge
}

export const dictFilter = (vue, name, needVal) => {
  if (!needVal) {
    return ''
  }
  if (!vue.$store.state.publicOption[name]) {
    throw new Error('请初始化相应字典字段: ' + name)
  } else if (vue.$store.state.publicOption[name].length === 0) {
    throw new Error('请在页面中初始化字典: ' + name)
  }
  const getArr = vue.$store.state.publicOption[name].find(val => {
    // eslint-disable-next-line eqeqeq
    return val.value == needVal
  })
  if (!getArr) {
    return ''
  }
  return getArr.text
}

// 把枚举值转成中文显示返回状态对应中文名称 封装版
export const statusCnName = (items, value, keys, rKeys) => {
  let data = ''
  let arr = []
  for (var i of items) {
    arr = Object.keys(i)
    break
  }
  for (var k of arr) {
    if (k === keys) {
      for (var j of items) {
        if (j[keys] === value) {
          data = j[rKeys]
        }
      }
    }
  }
  if (data !== '') {
    return data
  } else {
    return value
  }
}

// 获取两日期之间日期列表函数
export const getdiffdate = (stime, etime) => {
  // 初始化日期列表，数组
  var diffdate = []
  var i = 0
  // 开始日期小于等于结束日期,并循环
  while (stime <= etime) {
    diffdate[i] = stime
    // 获取开始日期时间戳
    var stimeTs = new Date(stime).getTime()
    // 增加一天时间戳后的日期
    var nextDate = stimeTs + (24 * 60 * 60 * 1000)
    // 拼接年月日，这里的月份会返回（0-11），所以要+1
    var nextDatesY = new Date(nextDate).getFullYear() + '-'
    var nextDatesM = (new Date(nextDate).getMonth() + 1 < 10) ? '0' + (new Date(nextDate).getMonth() + 1) + '-' : (new Date(nextDate).getMonth() + 1) + '-'
    var nextDatesD = (new Date(nextDate).getDate() < 10) ? '0' + new Date(nextDate).getDate() : new Date(nextDate).getDate()
    stime = nextDatesY + nextDatesM + nextDatesD
    // 增加数组key
    i++
  }
  return diffdate
}

// 阿拉伯数字转中文数字
export const numToChinese = (num) => {
  var arr1 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  var arr2 = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']// 可继续追加更高位转换值
  if (!num || isNaN(num)) {
    return '零'
  }
  var english = num.toString().split('')
  var result = ''
  for (var i = 0; i < english.length; i++) {
    var desi = english.length - 1 - i// 倒序排列设值
    result = arr2[i] + result
    var arr1index = english[desi]
    result = arr1[arr1index] + result
  }
  // 将【零千、零百】换成【零】 【十零】换成【十】
  result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十')
  // 合并中间多个零为一个零
  result = result.replace(/零+/g, '零')
  // 将【零亿】换成【亿】【零万】换成【万】
  result = result.replace(/零亿/g, '亿').replace(/零万/g, '万')
  // 将【亿万】换成【亿】
  result = result.replace(/亿万/g, '亿')
  // 移除末尾的零
  result = result.replace(/零+$/, '')
  // 将【零一十】换成【零十】
  // result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
  // 将【一十】换成【十】
  result = result.replace(/^一十/g, '十')
  return result
}

// 预览
// export const previewFileUrl = (file, t, i) => {
//   if (i - 0 === 1) {
//     t.loadingUpload1 = true
//   } else {
//     t.loadingUpload = true
//   }
//   // http://172.16.23.10:8012/onlineFileName?url=http://172.16.23.5:8848/jeecg-boot/downloadFilePreview/1437969000490332162.xlsx
//   // window.open('http://szbwgy.cn:9999/fileview/onlinePreview?url='+encodeURIComponent(Base64.encode(url)), '_blank')
//   const suffix = file.response.result.name.substr(file.response.result.name.lastIndexOf('.'))
//   const id = file.response.result.fileId
//   console.log(suffix)
//   if (suffix === '.jpeg' || suffix === '.png' || suffix === '.jpg' || suffix === '.gif') {
//     previewFile(id).then(res => {
//       if (res.code !== 200) {
//         t.$message.error('预览失败！')
//         return null
//       }
//       t.$message.destroy()
//       if (i - 0 === 1) {
//         t.loadingUpload1 = true
//       } else {
//         t.loadingUpload = true
//       }
//       window.open(res.message, '_blank')
//     }).catch(error => {
//       if (!error) {
//         t.$message.error('预览失败！')
//         if (i - 0 === 1) {
//           t.loadingUpload1 = true
//         } else {
//           t.loadingUpload = true
//         }
//       }
//     })
//   } else {
//     if (i - 0 === 1) {
//       t.loadingUpload1 = true
//     } else {
//       t.loadingUpload = true
//     }
//     window.open(`http://172.16.23.10:8012/onlineFileName?url=http://172.16.23.5:8848/jeecg-boot/downloadFilePreview/${id}${suffix}`, '_blank')
//   }
// }

// 图片是否有效
export const toImg = (img) => {
  var ImgObj = new Image()
  ImgObj.src = img
  var url = ''
  if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
    url = img
    return url
  } else {
    url = ''
    return url
  }
}

// 时间戳 + 随机四个大字字母
export const getRanNum = (name) => {
  // const name = 123.png
  // const NewName = _this.getRanNum() + name.substr(name.lastIndexOf('.'))
  const newData = Date.parse(new Date())
  var result = []
  for (var j = 0; j < 4; j++) {
    var ranNum = Math.ceil(Math.random() * 25)
    result.push(String.fromCharCode(65 + ranNum))
  }
  var name = ''
  name = newData + ''
  for (var i = 0; i < result.length; i++) {
    const desc = Math.ceil(Math.random() * 10)
    name = name.slice(0, desc) + result[i] + name.slice(desc)
  }
  return name
}